import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_framework/utils/logs.dart';

class TestCompute extends StatefulWidget {
  const TestCompute({super.key});

  @override
  State<TestCompute> createState() => _TestComputeState();
}

class _TestComputeState extends State<TestCompute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "测试compute方法",
        ),
      ),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () async {
                var res = await compute(sum, 1000000000000000000);
                logs("isolate 计算的数据 $res");
              },
              child: Text("计算"),
            )
          ],
        ),
      ),
    );
  }

  static int sum(int num) {
    int count = 0;
    while (num > 0) {
      count = count + num;
      num--;
    }
    return count;
  }
}
